/**
*	插件：弹出选择数据
*	开发者：雨中磐石
*	地址：www.rockoa.com
*	日期：2023-07-25 17:25
*/

js.selectdata = {
	init:function(){
		
	},
	create:function(cans){
		var me = this;
		var defv = js.applyIf(cans,{
			rand:js.getrand(),
			data:[],
			cnum:'',
			maxshow:100,
			width:450,
			onselect:function(){},
			souajax:'0',
			onrenderdata:function(d){return d;},
			page:1,
			title:''
		});
		for(var i in defv)this[i]=defv[i];
		var rand  = this.rand;
		
		this._init	= function(){
			this.ismobile = js.web.ismobile();
			this._showcreate();
		};
		
		this._showcreate = function(){
			
			var hei  = winHb()-200;
			if(hei>465)hei = 465;
			
			var s='	<div style="height:40px;overflow:hidden;border-bottom:1px #cccccc solid;padding:0px 0px"><table width="100%" style="background:white"><tr><td><select id="selxuan_'+this.rand+'" style="width:120px;border:none;background:none;display:none"><option value="">选择所有</option></select></td><td width="100%" height="40"><input id="changekey_'+this.rand+'" placeholder="搜索关键词" style="height:30px;border:none;background:none;width:100%;margin:0px 10px;outline:none"></td><td nworap><button class="cursor" style="background:none;color:#666666;border:none;width:50px" id="changesoubtn_'+this.rand+'" type="button" >查找</button></td></tr></table></div>';
			
			s+='<div style="-webkit-overflow-scrolling:touch;height:'+hei+'px;overflow:auto;" id="selectlist_'+rand+'" class="rock-panel"></div>';
			
			var act = 'select'+rand+'';
			this.view = act;
			js.modal.main({
				title:this.title,
				action:act,
				width:this.width,
				height:500,
				btn:[{text:'<i class="icon-refresh"></i>',bgcolor:';background:none;border:none;',cls:'webbtn-default'},{text:'确定'}],
				html:s
			});
			this.loaddata();
			$('#'+act+'_btn1').click(function(){
				me.queding();
			});
			$('#'+act+'_btn0').click(function(){
				me.loaddata();
			});
		}
		
		this.hide = function(){
			js.modal.close(this.view);
		}
		
		this.queding=function(){
			var ns= 'changeuserinput_'+rand+'';
			var o = $("input[name='"+ns+"']");
			var i,len=o.length,o1,xna,xu,xal,sid='',sna='',seld=[];
			for(i=0;i<len;i++){
				o1 = $(o[i]);
				if(o[i].checked){
					xna= o1.attr('xname');
					xu = parseFloat(o1.attr('xu'));
					if(this.checked){
						seld.push(this.nowdata[xu]);
					}else{
						seld=this.nowdata[xu];
					}
					xal= o1.val();
					sid+=','+xal+'';
					sna+=','+xna+'';
				}
			}
			if(sid!=''){
				sid=sid.substr(1);
				sna=sna.substr(1);
			}
			if(this.idobj)this.idobj.value=sid;
			if(this.nameobj){
				this.nameobj.value=sna;
				this.nameobj.focus();
			}
			this.onselect(seld,sna, sid);
			this.hide();
		};
		
		this.loaddata = function(){
			$('#selectlist_'+rand+'').html('<div align="center" style="padding-top:50px" ><i style="height:40px;width:40px" class="rock-loading"></i></div>');
			var url = this.url;
			var jg  = (url.indexOf('?')==-1) ? '?':'&';
			url+=''+jg+'page='+this.page+'';
			js.ajax.get({
				url:url,
				cnum:this.cnum,
				onsuccess:function(ret){
					me.data = ret;
					me.showdata(ret.data, true);
				},
				onerror:function(ret){
					$('#selectlist_'+rand+'').html(ret.msg);
				}
			});
		}
		
		this.showdata=function(a,inb){
			if(!a)a=[];
			var ret = a;
			//this.showselectdata(a.selectdata);
			var len = 0;
			if(a.totalCount)len=a.totalCount;
			if(a.rows)a = a.rows;
			if(len==0)len=a.length;
			this.souajax = a.souajax;
			var s='',s1='',npid='nextpage_'+this.rand+'';
			if(!len || len==0){
				s='<div align="center" style="margin-top:30px;color:#cccccc;font-size:16px">无记录</div>';
			}else{
				s = this.showhtml(a, 0);
				s1='共'+len+'条';
				if(ret.page && ret.limit){
					var max = Math.ceil(len/ret.limit);
					s1+=' ';
					if(ret.page>1)s1+='<a id="'+npid+'1" class="zhu" href="javascript:;">上页</a>';
					s1+='('+ret.page+'/'+max+')';
					if(max>ret.page)s1+='<a id="'+npid+'" class="zhu" href="javascript:;">下页</a>';
				}
			}
			
			var o = $('#selectlist_'+rand+'');
			o.html(s);
			$('#msgview_'+this.view+'').html(s1);
			if(s1){
				$('#'+npid+'').click(function(){me.nextpage(1);});
				$('#'+npid+'1').click(function(){me.nextpage(-1);});
			}
		};
		
		this.nextpage=function(p){
			this.page = this.page+p;
			this.loaddata(this.selvalue);
		};
		
		this.showhtml=function(a,ks){
			this.nowdata = a;
			var i,len=a.length,s='',s2,s1='',atr,oldvel='',d;
			if(this.nameobj)oldvel=this.nameobj.value;
			if(this.idobj)oldvel=this.idobj.value;
			var type='checkbox',ched='';
			if(!ks)ks=0;
			if(!this.checked)type='radio';
			oldvel = ','+oldvel+',';
			for(i=ks;i<len && i<this.maxshow+ks;i++){
				ched='';
				d = this.onrenderdata(a[i]);
				if(!isempt(d.value) && oldvel.indexOf(','+d.value+',')>-1)ched='checked';
				if(d.disabled)ched+=' disabled';
				s2 = '<input xu="'+i+'" '+ched+' name="changeuserinput_'+rand+'" xname="'+d.name+'" value="'+d.value+'" style="width:18px;height:18px;" align="absmiddle" type="'+type+'">';
				atr = '';
				if(d.padding)atr='style="padding-left:'+d.padding+'px"';
				if(!d.iconswidth)d.iconswidth=18;
				if(d.iconsimg)s2+=' <img align="absmiddle" src="'+d.iconsimg+'" height="'+d.iconswidth+'" width="'+d.iconswidth+'">';
				s+='<div class="rock-list-item rock-hover" '+atr+'><label>'+s2+'&nbsp;'+d.name+'';
				if(d.subname)s+='&nbsp;<span style="font-size:12px;color:#888888">('+d.subname+')</span>';
				s+='</label></div>';
			}
			if(len>i){
				s+='<div align="center" id="moreadd_'+rand+'" style="padding:10px;font-size:12px;color:#aaaaaa;cursor:pointer">还有'+(len-i)+'条，点我加载</div>';
				setTimeout(function(){$('#moreadd_'+rand+'').click(function(){me.moregegd(i);});},10);
			}
			return s;
		};
		
		this._init();
	}
}